<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>

<body>
    <h2>历史搜索</h2>
    <input type="text" id="input">
    <div id="search"></div>
    <script>
        var historys = [];
        $("#input").keyup(function (event) {
            if (event.keyCode == 13) {
                if (this.value != '' && historys.indexOf(this.value) == -1) {
                    var p = document.createElement('p');
                    p.innerHTML = this.value;
                    $("#search").prepend(p);
                    historys.unshift(this.value);
                    $(this).val('');
                    // console.log(historys)
                } else {
                    alert("您输入的值为空或您输入的值已存在");
                    $(this).val('');

                }
            }
        })
    </script>
</body>

</html>